// ml:run = $bin < input
#include <iostream>
#include <algorithm>

char map[200][200];
int n, m;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin >> n >> m;
    auto left = m;
    auto right = 0;
    auto up = n;
    auto down = 0;
    auto tot = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            char ch; std::cin >> ch;
            map[i][j] = ch;
            if (ch == 'B') {
                left = std::min(left, j);
                right = std::max(right, j);
                up = std::min(up, i);
                down = std::max(down, i);
                tot++;
            }
        }
    if (!tot) { std::cout << "1\n"; return 0; }

    auto ans = -1;
    auto mm = right - left + 1;
    auto nn = down - up + 1;
    // std::cout << nn << " " << mm << "\n";
    if (nn > mm) {
        ans = nn * nn - tot;
        if (nn > m) ans = -1;
    } else {
        ans = mm * mm - tot;
        if (mm > n) ans = -1;
    }
    std::cout << ans << "\n";
}

